package org.jimmy.tang.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * @author jimmy
 * 
 */
public class ReflectionAPIs {

    /**
     * 
     * @param c
     * @throws ClassNotFoundException
     */
    public void packageInfoAPI(Class<?> c) throws ClassNotFoundException {
        Package p;
        p = c.getPackage();
        if (p != null) {
            System.out.println("package " + p.getName() + ";");
        }
    }

    /**
     * 
     * @param c
     */
    public void fieldInfoAPI(Class<?> c) {
        Field[] ff = c.getDeclaredFields();
        for (int i = 0; i < ff.length; i++) {
            System.out.println(ff[i].getName());
            // x = tName(ff[i].getType().getName(), classRef);
        }
    }

    /**
     * 
     * @param c
     */
    public void constructorInfoAPI(Class<?> c) {
        Constructor<?>[] cn = c.getDeclaredConstructors();
        for (int i = 0; i < cn.length; i++) {
            System.out.println("Constructor :" + cn[i]);
            Class<?> cx[] = cn[i].getParameterTypes();
            for (int j = 0; j < cx.length; j++) {
                System.out.println("Constructor parameter : " + cx[j]);
                // x = tName(cx[j].getName(), classRef);
            }

        }
    }

    /**
     * 
     * @param c
     * @param setMethodName
     * @param getMethodName
     * @param value
     * @param parameters
     * @throws Exception
     */
    public void setAndGetMethod(Class<?> c, String setMethodName,
            String getMethodName, Object value, Class<?>... parameters)
            throws Exception {
        Object student = c.newInstance();

        Method setMethod = c.getDeclaredMethod(setMethodName, parameters);
        setMethod.invoke(student, value);// 调用set方法

        Method getMethod = c.getDeclaredMethod(getMethodName);
        System.out.println(getMethod.invoke(student));// 再调用get方法<pre></pre>
    }

    /**
     * 
     * @param c
     * @param fieldName
     * @param fieldValue
     */
    public void setPublicFiled(Class<?> cls, String fieldName, Object fieldValue)
            throws Exception {
        Object student = cls.newInstance();// 得到一个实例
        Field field = cls.getDeclaredField(fieldName);
        field.set(student, fieldValue);
        System.out.println(field.get(student));
    }

    /**
     * 
     * @param cls
     * @param fieldName
     * @param fieldValue
     * @throws Exception
     */
    public void setPrivateField(Class<?> cls, String fieldName,
            Object fieldValue) throws Exception {
        Object student = cls.newInstance();
        Field field = cls.getDeclaredField(fieldName);
        field.setAccessible(true);// 设置允许访问
        field.set(student, fieldValue);
        System.out.println(field.get(student));
    }

    /**
     * 
     * @param c
     * @param methodName
     * @param cs
     * @param objs
     * @throws Exception
     */
    public void singleMethodAPI(Class<?> c, String methodName, Class<?>[] cs,
            Object... objs) throws Exception {
        Method m = c.getDeclaredMethod(methodName, cs);
        m.invoke(c.newInstance(), objs);
    }

    /**
     * 
     * @param c
     * @param methodName
     * @param cs
     * @param objs
     * @throws Exception
     */
    public void staticMethodInvoke(Class<?> c, String methodName,
            Class<?>[] cs, Object... objs) throws Exception {
        Method m = c.getDeclaredMethod(methodName, cs);
        m.invoke(c, objs);
    }

    /**
     * 
     * @param c
     */
    public void methodsInfoAPI(Class<?> c) {
        System.out.println("---------------------------------");
        Method[] mm = c.getDeclaredMethods();

        for (int i = 0; i < mm.length; i++) {

            System.out.println("Method :" + mm[i]);

            Class<?> cx[] = mm[i].getParameterTypes();

            for (int j = 0; j < cx.length; j++) {
                System.out.println("Method parameter :" + cx[j]);
                // x = tName(cx[j].getName(), classRef);
            }

        }
    }

    /**
     * 
     * @param c
     */
    public void modifierInfoAPI(Class<?> c) {
        System.out.println("---------------------------------");
        int mod = c.getModifiers();
        System.out.println(Modifier.toString(mod));

        if (Modifier.isInterface(mod)) {
            System.out.println(" ");
        } else {
            System.out.println(" class ");
        }

        // System.out.print(tName(c.getName(), null));
    }

    /**
     * 
     * @param c
     */
    public void typeParameterInfoAPI(Class<?> c) {

    }

}
